﻿using CubicFWNet.Application.SystemManager;
using CubicFWNet.Common;
using CubicFWNet.Data.Models;
using CubicFWNet.Mvc.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CubicFWNet.Mvc.Controllers
{
    public class ItemController : ControllerBase
    {
        private ItemApp _itemApp;
        public ItemController()
        {
            _itemApp = new ItemApp();
        }
          [Authenticate]
        [HttpGet]
        public  ViewResult Index(string id)
        {
            var category = Guid.Empty;
            if (!string.IsNullOrEmpty(id))
            {
                category = Guid.Parse(id);
            }
            ViewBag.CategoryItemId = id;

            ViewBag.CategoryItems = _itemApp.GetItemCategory();
            return View(_itemApp.GetItems(category));
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(Guid ItemId)
        {
            var data = _itemApp.GetItem(ItemId);
            return Content(data.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public string Delete(Guid id)
        {
            try
            {
                if (_itemApp.Delete(id) == -404)
                {
                    Result.Status = "error";
                    Result.Message = "未找到该数据";
                    Result.ToJson();
                }
            }
            catch (Exception e)
            {
                Result.Status = "error";
                Result.Message = e.Message;
            }
            return Result.ToJson();
        }

        //添加或修改角色
        [HttpPost]
        [HandlerAjaxOnly]
        public string Add(Item item)
        {
            try
            {
                item.CreatorId = operatorModel.UserId;
                _itemApp.AddOrUpdate(item);
            }
            catch (Exception ex)
            {
                Result.Status = "error";
                Result.Message = ex.Message;
            }
            return Result.ToJson();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetItemCategoryJson()
        {
            var data = _itemApp.GetItemCategory();
            var treeList = new List<TreeViewModel>();
            treeList.Add(new TreeViewModel()
            {
                id = Guid.Empty.ToString(),
                text = "全部",
                value = "",
                parentId = "",
                isexpand = true,
                complete = true,
                hasChildren = false
            });
            foreach (ItemCategory item in data)
            {
                TreeViewModel tree = new TreeViewModel();
                tree.id = item.Id.ToString();
                tree.text = item.Name;
                tree.value = item.Id.ToString();
                tree.parentId = "";
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = false;
                treeList.Add(tree);
            }
            return Content(treeList.TreeViewJson());
        }

    }
}